package com.symone.webcms.schedule;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 int state = scheduler.getTriggerState(triggerName, triggerGroup);
 STATE_BLOCKED 4 阻塞
 STATE_COMPLETE 2 完成
 STATE_ERROR 3 错误
 STATE_NONE -1 不存在
 STATE_NORMAL 0 正常
 STATE_PAUSED 1 暂停
 **/

public class ScheduleJobListener implements JobListener {

    protected Logger logger = LoggerFactory.getLogger(ScheduleJobListener.class);

    @Override
    public String getName() {
        return this.getClass().getName();
    }

    /**
     * (1)
     * 任务执行之前执行
     * @param jobExecutionContext
     */
    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        logger.info("ScheduleJobListener.jobToBeExecuted()");
    }

    /**
     * (2)
     * 这个方法正常情况下不执行,但是如果当TriggerListener中的vetoJobExecution方法返回true时,那么执行这个方法.
     * 需要注意的是 如果方法(2)执行 那么(1),(3)这个俩个方法不会执行,因为任务被终止了嘛.
     * @param jobExecutionContext
     */
    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
        logger.info("ScheduleJobListener.jobExecutionVetoed()");
    }

    /**
     * (3)
     * 任务执行完成后执行,jobException如果它不为空则说明任务在执行过程中出现了异常
     * @param jobExecutionContext
     * @param e
     */
    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
        logger.info("ScheduleJobListener.jobWasExecuted()");
    }
}
